package com.shixi.shixitraining.shixitraining.ui.tabhome.activity;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.shixi.shixitraining.shixitraining.R;
import com.shixi.shixitraining.shixitraining.base.BaseActivity;
import com.shixi.shixitraining.shixitraining.http.CallbackImple;
import com.shixi.shixitraining.shixitraining.http.HttpUtil;
import com.shixi.shixitraining.shixitraining.test.TestPopup;
import com.shixi.shixitraining.shixitraining.ui.tabhome.adapter.PopupAuthenAdapter;
import com.shixi.shixitraining.shixitraining.ui.tabhome.adapter.TrainAdapter;
import com.shixi.shixitraining.shixitraining.ui.tabhome.entity.TrainAuthenBean;
import com.shixi.shixitraining.shixitraining.util.DensityUtil;
import com.shixi.shixitraining.shixitraining.util.TitleUtil;
import com.shixi.shixitraining.shixitraining.util.ToastUtil;
import com.shixi.shixitraining.shixitraining.view.recycleview.RecycleDivider;
import com.shixi.shixitraining.shixitraining.view.recycleview.RecycleItemClickListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import retrofit2.Call;

import static com.shixi.shixitraining.shixitraining.constant.Constants.TRAIN;

public class TrainingAuthenActivity extends BaseActivity implements View.OnClickListener {


    private PopupWindow popupWindow;
    private TextView mCompiste;
    private float alpha = 1.0f;
    private List<TrainAuthenBean.DataBean.TypeBean> type;
    private List<TrainAuthenBean.DataBean.SortBean> sort;
    private List<TrainAuthenBean.DataBean.CourseBean> course;

    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    backgroundAlpha((Float) msg.obj);
                    break;
            }
            return false;
        }
    });
    private PopupWindow coursePopup;
    private TextView mCourse;
    private TextView mMajor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_training_certification);
        ButterKnife.bind(this);
        initTitle();
        initView();
        initBaseData();
    }

    private void initBaseData() {


        HttpUtil.getInstance().getApiInterface().TrainAuthen(TRAIN+"/train/index").enqueue(new CallbackImple<TrainAuthenBean>() {
            @Override
            public void onError(Call<TrainAuthenBean> call, Throwable t) {
                ToastUtil.showShortToast(TrainingAuthenActivity.this,t.getMessage());
            }

            @Override
            public void onSuccess(Call<TrainAuthenBean> call, TrainAuthenBean response) {

                if (response.getStatus()==200){

                    if (response.getData()!=null&&response!=null){

                        final List<TrainAuthenBean.DataBean.ListsBean>  baseData = response.getData().getLists();
                        type = response.getData().getType();
                        sort = response.getData().getSort();
                        course = response.getData().getCourse();
                        RecyclerView mRcview = (RecyclerView) findViewById(R.id.rcview_training_certification);
                        GridLayoutManager manager=new GridLayoutManager(TrainingAuthenActivity.this,2);
                        manager.setOrientation(LinearLayoutManager.VERTICAL);
                        mRcview.setLayoutManager(manager);
                        TrainAdapter adapter=new TrainAdapter(TrainingAuthenActivity.this,baseData, Glide.with(TrainingAuthenActivity.this));
                        mRcview.setAdapter(adapter);
                        mRcview.addOnItemTouchListener(new RecycleItemClickListener(TrainingAuthenActivity.this) {
                            @Override
                            public void itemClickListener(View view, int position) {
                                TrainAuthenBean.DataBean.ListsBean listsBean = baseData.get(position);
                                String id = listsBean.getId();
                                Bundle bundle=new Bundle();
                                bundle.putString("trainId",id);
                                Intent intent=new Intent(TrainingAuthenActivity.this,AuthenDetailActivity.class);
                                intent.putExtras(bundle);
                                startActivity(intent);
                            }
                        });
                    }
                }else {
                    ToastUtil.showShortToast(TrainingAuthenActivity.this,response.getMsg());
                }

            }
        });
    }

    private void initTitle() {

        TitleUtil titleUtil = new TitleUtil(findViewById(R.id.top_bar_layout));
        titleUtil.initTitle(4, R.id.top_bar_center_tv, R.id.top_bar_left_img, 0,
                R.drawable.icon_back, 0, "培训认证", "", "");
        ImageView mBackImg = (ImageView) findViewById(R.id.top_bar_left_img);
        mCourse = (TextView) findViewById(R.id.tv_course);
        mCompiste = (TextView) findViewById(R.id.tv_compiste);
        mMajor = (TextView) findViewById(R.id.tv_major);
        mCompiste.setOnClickListener(this);
        mCourse.setOnClickListener(this);
        mMajor.setOnClickListener(this);
        mBackImg.setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.top_bar_left_img:
                this.finish();
                break;
            case R.id.tv_compiste:
                initPopup();
                delayPopup();
                break;
            case R.id.tv_course:
                initCoursePopup();
                break;
            case R.id.tv_major:
                initMahorPopup();
                break;

        }
    }

    //专业类型
    private void initMahorPopup() {


    }
//课程属性
    private void initCoursePopup() {


        View mContentView = getLayoutInflater().inflate(R.layout.item_session, null);
        int screenWidthPixels = DensityUtil.getScreenWidthPixels(this);
        if (coursePopup==null){
            coursePopup = new PopupWindow(mContentView,screenWidthPixels, DensityUtil.dp2px(160f));
            coursePopup.setBackgroundDrawable(new ColorDrawable(0x0000000));
        }
        coursePopup.setContentView(mContentView);
        coursePopup.showAsDropDown(mCourse);

        RecyclerView mCourseView = (RecyclerView) mContentView.findViewById(R.id.popup_session_list);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        RecycleDivider divider = new RecycleDivider(new ColorDrawable(ContextCompat.getColor(this, R.color.gray)), OrientationHelper.VERTICAL);
        divider.setHeight(this, 0.5f);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        mCourseView.setLayoutManager(manager);
        mCourseView.addItemDecoration(divider);

        List<TestPopup>  data=new ArrayList<>();
        data.add(new TestPopup("不限"));
        data.add(new TestPopup("企业管理"));
        data.add(new TestPopup("职业认证"));

        PopupAuthenAdapter adapter=new PopupAuthenAdapter(this,data);
        mCourseView.setAdapter(adapter);

    }


    //初始化列表
    private void initView() {
        RecyclerView mRcView = (RecyclerView) findViewById(R.id.rcview_training_certification);
        GridLayoutManager manager = new GridLayoutManager(this, 2);
        mRcView.setLayoutManager(manager);

//        List<HomeGrideAuthenBean> data = new ArrayList<>();
//        data.add(new HomeGrideAuthenBean("http://www.shixi.com/data/comads/2018/03/30/1522376843671.jpg", "课程课程课程", "职业技术证书", "2988.00", 2, 2));
//        data.add(new HomeGrideAuthenBean("http://www.shixi.com/data/comads/2018/03/30/1522376843671.jpg", "课程课程课程", "职业技术证书", "2988.00", 2, 2));
//        data.add(new HomeGrideAuthenBean("http://www.shixi.com/data/comads/2018/03/30/1522376843671.jpg", "课程课程课程", "职业技术证书", "2988.00", 2, 2));
//        data.add(new HomeGrideAuthenBean("http://www.shixi.com/data/comads/2018/03/30/1522376843671.jpg", "课程课程课程", "职业技术证书", "2988.00", 2, 2));
//        data.add(new HomeGrideAuthenBean("http://www.shixi.com/data/comads/2018/03/30/1522376843671.jpg", "课程课程课程", "职业技术证书", "2988.00", 2, 2));
//        data.add(new HomeGrideAuthenBean("http://www.shixi.com/data/comads/2018/03/30/1522376843671.jpg", "课程课程课程", "职业技术证书", "2988.00", 2, 2));
//
//        HomeAuthenGrideAdapter adapter = new HomeAuthenGrideAdapter(this, data, Glide.with(this));
//        mRcView.setAdapter(adapter);
//        mRcView.addOnItemTouchListener(new RecycleItemClickListener(this) {
//            @Override
//            public void itemClickListener(View view, int position) {
//                startActivity(new Intent(TrainingAuthenActivity.this,AuthenDetailActivity.class));
//            }
//        });

    }


    private void initPopup() {

        View mContentView = getLayoutInflater().inflate(R.layout.authen_popupwindown_composite, null);
        if (popupWindow == null) {
            int screenWidthPixels = DensityUtil.getScreenWidthPixels(this);
            int screenHeightPixels = DensityUtil.getScreenHeightPixels(this);
            popupWindow = new PopupWindow(mContentView, screenWidthPixels, WindowManager.LayoutParams.WRAP_CONTENT);
            popupWindow.setBackgroundDrawable(new ColorDrawable(0x0000000));
        }

        popupWindow.setFocusable(false);
        popupWindow.setContentView(mContentView);
        popupWindow.showAsDropDown(mCompiste);
//      popupWindow.showAtLocation(mCompiste, Gravity.CENTER | Gravity.CENTER_HORIZONTAL, 0, 0);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        while (alpha < 1.0f) {
                            Message msg = handler.obtainMessage();
                            msg.what = 1;
                            alpha += 0.01f;
                            msg.obj = alpha;
                            handler.sendMessage(msg);
                        }
                    }
                }).start();
            }
        });

    }

    //设置popupwindown的背景颜色
    private void backgroundAlpha(float bgAlpha) {

        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = bgAlpha;
        getWindow().setAttributes(lp);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

    }


    private void delayPopup() {

        new Thread(new Runnable() {
            @Override
            public void run() {
              while (alpha>0.5f){
                  Message msg = handler.obtainMessage();
                  msg.what=1;
                  alpha-=0.01f;
                  msg.obj=alpha;
                  handler.sendMessage(msg);
              }
            }
        }).start();

    }

}
